// 生成首页路由
export function generateIndexRouter(data) {
  let indexRouter = []
  for (var item of data) {
    console.log(data);
    let obj = {
      path: item.path,
      name: item.name,
      component: resolve => require(['@/components/layouts/Home'], resolve),
      meta: {
        title: item.meta.title,
      },
      redirect: item.children ? item.children[0].path : '',
      children: [
        ...generateChildRouters(item.children ? item.children : [])
      ]
    }
    indexRouter.push(obj)
  }
  return indexRouter
}

// 生成嵌套路由（子路由）
function generateChildRouters(data) {
  const routers = []
  for (var item of data) {
    let component = ''
    if (item.component.indexOf('layouts') >= 0) {
      component = 'components/' + item.component
    } else {
      component = 'views/' + item.component
    }

    let menu = {
      path: item.path,
      realPath: item.path,
      name: item.name,
      component: resolve => require(['@/' + component + '.vue'], resolve),
      meta: {
        title: item.meta.title,
      }
    }
    if (item.children && item.children.length > 0) {
      menu.children = [...generateChildRouters(item.children)]
      menu.redirect = menu.children[0].path
    }
    // 判断是否生成路由
    if (item.route && item.route === '0') {
      console.log(' 不生成路由 item.route：  ' + item.route)
      console.log(' 不生成路由 item.path：  ' + item.path)
    } else {
      routers.push(menu)
    }
  }
  return routers
}